package com.goethe.utils.db;

import java.util.ArrayList;
import java.util.List;

import know.how.glutenfree.RestaurantDetailsItem;
import know.how.glutenfree.RestaurantItem;


import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DatabaseAccessor {

	public static DataBaseHelper myDbHelper = null;
	public static SQLiteDatabase rdb = null;
	public static SQLiteDatabase wdb = null;

	public static synchronized final void initDB(Context context)
			throws Exception {
		if (myDbHelper == null) {
			myDbHelper = new DataBaseHelper(context);
			myDbHelper.openDataBase();
			rdb = myDbHelper.getReadableDatabase();
			wdb = myDbHelper.getWritableDatabase();
		}
	}

	public static synchronized final void closeDB() {
		if (myDbHelper != null) {
			myDbHelper.close();
			rdb.close();
			wdb.close();
			myDbHelper = null;
			rdb = null;
			wdb = null;
		}
	}
	
	public static final List<RestaurantItem> getRestaurants(){
		List<RestaurantItem> items = new ArrayList<RestaurantItem>();
		try {
			String[] columns = { "ID","Description","IsFavorite","Img" };
			String tableName = "Restaurant";
			Cursor cursor = rdb.query(tableName, columns, null, null, null,
					null, "Description ASC");
			RestaurantItem li;
			while (cursor.moveToNext()) {
				li = new RestaurantItem();
				li.setId(cursor.getInt(0));
				li.setTitle(cursor.getString(1));
				li.setFavorite(cursor.getShort(2)!=0);
				li.setDrawableName(cursor.getString(3));
				items.add(li);
			}
			cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return items;
	}
	
	public static final List<RestaurantDetailsItem> getRestaurantDetails(int restaurantId){
		List<RestaurantDetailsItem> items = new ArrayList<RestaurantDetailsItem>();
		try {
//			String qry = "SELECT verses.id,verses.content,%s.content FROM verses,%s  WHERE ((id=verses_id) AND (sura_id=%d)) ORDER BY id";
			String qry = "SELECT ID,title, subtitle,Detail,Footer,UnderlineIndicator,Image from RestaurantDetail where RestaurantID = %d ORDER BY ID ASC";
			qry = String.format(qry,restaurantId);
			Log.i("DREG", "QRY=" + qry);
			Cursor cursor = rdb.rawQuery(qry, null);
			RestaurantDetailsItem rdi;
			while (cursor.moveToNext()) {
				rdi = new RestaurantDetailsItem();
				rdi.setId(cursor.getInt(0));
				rdi.setTitle(cursor.getString(1));
				rdi.setSubTitle(cursor.getString(2));
				rdi.setDetails(cursor.getString(3));
				rdi.setFooter(cursor.getString(4));
				rdi.setUnderlineIndicator(cursor.getString(5));
				rdi.setImage(cursor.getString(6));
				items.add(rdi);
			}
			cursor.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return items;
	}
	
	public static void addToFavorite(RestaurantItem listItem){
		try {
			String tableName = "Restaurant";
			ContentValues values = new ContentValues();
			values.put("IsFavorite", true);
			Log.i("DREG", "UPDATED="+wdb.update(tableName, values, " ID = " + listItem.getId()+"", null));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public static void remiveFromFavorite(RestaurantItem listItem){
		try {
			String tableName = "Restaurant";
			ContentValues values = new ContentValues();
			values.put("IsFavorite", false);
			Log.i("DREG", "UPDATED="+wdb.update(tableName, values, " ID = " + listItem.getId()+"", null));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	public static void updateImageFileName(String desc,String imageFile){
		try {
			String tableName = "Restaurant";
			ContentValues values = new ContentValues();
			values.put("Img", imageFile);
			Log.i("DREG", "UPDATED="+wdb.update(tableName, values, " Description = '" + desc+"'", null));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	
	
	//SELECT ID,title, subtitle,Detail from RestaurantDetail where RestaurantID = 3 ORDER BY ID ASC
}
